package com.yanqu.road.dao.impl.cross.banquet;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.baifubanquet.GoodBanquetFinishReceiveData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2021/11/16 15:39
 */
public class GoodBanquetFinishReceiveDataDaoImpl extends TempDataObjectDao<GoodBanquetFinishReceiveData> {
    public static String getGoodBanquetFinishReceiveDataSql = "select * from `t_u_activity_good_banquet_finish_receive` where isReceive = 0";
    public Map<Long, List<GoodBanquetFinishReceiveData>> getGoodBanquetFinishReceiveDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<GoodBanquetFinishReceiveData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGoodBanquetFinishReceiveDataSql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                GoodBanquetFinishReceiveData item = parse(rs);
                List list = map.get(item.getUserId());
                if(list == null){
                    list = new ArrayList();
                    map.put(item.getUserId(),list);
                }
                list.add(item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateGoodBanquetFinishReceiveDataSql = "update `t_u_activity_good_banquet_finish_receive` set userId=?,type=?,num=?,isReceive=?,endTime=? where banquetId=? and activityId=?";
    @Override
    protected boolean updateData(GoodBanquetFinishReceiveData goodBanquetFinishReceiveData) {
        goodBanquetFinishReceiveData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetFinishReceiveData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetFinishReceiveData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetFinishReceiveData.getNum()));
        para.put(i++, new DbParameter(Types.BOOLEAN, goodBanquetFinishReceiveData.getIsReceive()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetFinishReceiveData.getEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetFinishReceiveData.getBanquetId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetFinishReceiveData.getActivityId()));
        if (getDb().executeNoneQuery(updateGoodBanquetFinishReceiveDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGoodBanquetFinishReceiveDataSql = "insert into `t_u_activity_good_banquet_finish_receive`(banquetId,activityId,userId,type,num,isReceive,endTime) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(GoodBanquetFinishReceiveData goodBanquetFinishReceiveData) {
        goodBanquetFinishReceiveData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetFinishReceiveData.getBanquetId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetFinishReceiveData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetFinishReceiveData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetFinishReceiveData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetFinishReceiveData.getNum()));
        para.put(i++, new DbParameter(Types.BOOLEAN, goodBanquetFinishReceiveData.getIsReceive()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetFinishReceiveData.getEndTime()));
        if (getDb().executeNoneQuery(insertGoodBanquetFinishReceiveDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GoodBanquetFinishReceiveData parseData(ResultSet rs) throws SQLException {
        GoodBanquetFinishReceiveData goodBanquetFinishReceiveData = new GoodBanquetFinishReceiveData();
        goodBanquetFinishReceiveData.setBanquetId(rs.getLong("banquetId"));
        goodBanquetFinishReceiveData.setActivityId(rs.getInt("activityId"));
        goodBanquetFinishReceiveData.setUserId(rs.getLong("userId"));
        goodBanquetFinishReceiveData.setType(rs.getInt("type"));
        goodBanquetFinishReceiveData.setNum(rs.getInt("num"));
        goodBanquetFinishReceiveData.setIsReceive(rs.getBoolean("isReceive"));
        goodBanquetFinishReceiveData.setEndTime(rs.getLong("endTime"));

        return goodBanquetFinishReceiveData;
    }
}
